using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Mes.Productions.MaterialLoadings.Queries;

/// <summary>
/// 分页查询批次料上料记录命令。
/// </summary>
/// <param name="Sn">批次码</param>
public sealed record GetBatchMaterialLoadingListWithPagedQuery(
    string? Barcode,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<ProdBatchMaterialLoadingDto>>>;

internal sealed class GetBatchMaterialLoadingListWithPagedQueryHandler(IRepository<ProdBatchMaterialLoading> repo)
    : IRequestHandler<GetBatchMaterialLoadingListWithPagedQuery, IResult<PagedList<ProdBatchMaterialLoadingDto>>>
{
    public async Task<IResult<PagedList<ProdBatchMaterialLoadingDto>>> Handle(GetBatchMaterialLoadingListWithPagedQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Barcode), m => m.Barcode == request.Barcode)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<ProdBatchMaterialLoadingDto>>());
    }
}
